import Utils from './mod.utils';
import Dialog from './vue.dialog';

export default {
  permission: {
    location: 'android.permission.ACCESS_FINE_LOCATION',
    coarseLocaltion: 'android.permission.ACCESS_COARSE_LOCATION',
    storage: 'android.permission.WRITE_EXTERNAL_STORAGE',
  },
  version(_handler) {
    try {
      ncp.open((version) => {
        if (_handler && typeof _handler == "function") {
          _handler(version);
        }
      });
    } catch (e) {
      console.log(e);
    }
  },
  download(context, params) {
    let url = params.url ? params.url : '';
    let title = params.title ? params.title : '';
    let description = params.description ? params.description : '';
    let fileName = params.fileName ? params.fileName : '';
    let type = params.type ? params.type : '';
    let mime = Utils.getMimeBySuffix(type);
    // let mime = params.mime ? params.mime : 'application/!*';
    let successHandler = params.success ? params.success : null;
    let errorHandler = params.error ? params.error : null;

    if (url == '') {
      Dialog.toast(context, {text: '下载地址不能为空！'});
      return false;
    }
    if (fileName == '') {
      Dialog.toast(context, {text: '下载文件名不能为空！'});
      return false;
    }

    let target = {
      url: url,
      title: title,
      mime: mime,
      description: description,
      fileName: fileName,
    };
    console.log(JSON.stringify(target));

    let dialog = context.$f7.dialog.progress('正在下载...');
    try {
      ncp.permit.rall(function (ra) {
        console.log('request all: ' + ra);
      });
      ncp.open.download(target, function (result) {
        var id = result;
        console.log('start download id: ' + result);

        var iid = setInterval(function () {
          ncp.open.download.status(id, function (r) {
            if (r == 1) {
              dialog.close();
              // toast download done
              clearInterval(iid);
              Dialog.toast(context, {text: '下载完成'});
              if (successHandler != null) {
                successHandler();
              }
            }
            console.log('id #' + id + " status: " + r);
          }, function (error) {
            dialog.close();
            clearInterval(iid);
            // console.log('error: ' + error);
            // console.log(error);
            console.log('获取当前下载 id 为 ' + id + ' 的文件下载状态失败：' + error);
            if (errorHandler != null) {
              errorHandler('download-status', error);
            }
          })
        }, 500);
      }, function (error) {
        dialog.close();
        console.log('download failed: ' + error);
        if (errorHandler != null) {
          errorHandler('download', error);
        }
      });
      // ncp.open.download(target, function (result) {
      //   var id = result;
      //   console.log('start download id: ' + result);
      //
      //   var iid = setInterval(function () {
      //     ncp.open.dstatus(id, function (r) {
      //       if (r == 1) {
      //         // toast download done
      //         dialog.close();
      //         clearInterval(iid);
      //         if (successHandler != null)
      //           successHandler();
      //       }
      //
      //       console.log('id #' + id + " status: " + r);
      //     }, function (error) {
      //       clearInterval(iid);
      //       dialog.close();
      //       // console.log(error);
      //       console.log('获取当前下载 id 为 ' + id + ' 的文件下载状态失败：' + error);
      //       if (errorHandler != null)
      //         errorHandler('download-status', error);
      //     })
      //   }, 500);
      //
      // }, function (error) {
      //   dialog.close();
      //   console.log('download failed: ' + error);
      //   if (errorHandler != null)
      //     errorHandler('download', error);
      // });
    } catch (e) {
      console.log(e);
    }
  },
  browser: {
    open(url) {
      try {
        ncp.open.url(url, function (res) {
        });
      } catch (e) {
        console.log(e);
      }
    },
  },
  checkPermission(_permission, _handlerFunc) {
    let _this = this;
    try {
      ncp.permit.check(_permission, function (result) {
        console.log('=====check permission' + JSON.stringify(result));
        let res = {
          isHave: true,
        };
        if (_handlerFunc != null) {
          _handlerFunc(res);
        }
      }, function (err) {
        console.log('check permit error: ' + err);
        let res = {
          isHave: false,
        };
        if (_handlerFunc != null) {
          _handlerFunc(res);
        }
      });
    } catch (exception) {
      console.log(exception);
    }
  },
  requestPermission(_permission, _handlerFunc) {
    let _this = this;
    try {
      ncp.permit.request(_permission, function (result) {
        if (_handlerFunc != null) {
          _handlerFunc(result);
        }
      }, function (err) {
        console.log('request permit error: ' + err);
      });
    } catch (exception) {
      console.log(exception);
    }
  },
};